/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package client;

import common.Helper;
import common.IProcessManager;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 *
 */
public class EmittingThread implements Runnable {
    private IReceiveThread iReceiveThread;
    private Buffer buffer;
    private boolean running;
    Integer talkingToProcess;

    public EmittingThread(Buffer buffer) {
        this.buffer = buffer;
        this.running = true;
        this.talkingToProcess = null;
    }

    /* read the buffer and send the content read to the receiving thread
     */
    private void emit()
    {
        BufferMessage message = buffer.get();
        message.getParams().set(0, Helper.getProcessId().toString());
        message.getParams().set(1, talkingToProcess.toString());
        try
        {
            iReceiveThread.put(message);
        } catch (RemoteException ex)
        {
            Logger.getLogger(EmittingThread.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println(
                "[debug] "
                + "Process: " + Helper.getProcessId() + "; "
                + "Thread: " + Thread.currentThread().getName() + ";\n"
                + "[debug] "
                + "Action: emittingThread.emit()" + " -> " + message + "\n");
    }

    /**
     * gently stop the thread
     */
    public void stop()
    {
        running = false;
    }

    /**
     * Gets the remote listener RMI url and connects to it
     */
    public void run()
    {
        IProcessManager processManager = Helper.getProcessManager();
        talkingToProcess = null;
        try
        {
            // should lock until it gets another process to talk to
            talkingToProcess = processManager.onlyTalksTo(Helper.getProcessId());
        } catch (RemoteException ex)
        {
            Logger.getLogger(EmittingThread.class.getName()).log(Level.SEVERE, null, ex);
        }
        // will look like "rmi://localhost/processX/receiveBuffer"
        String remoteReceiveBufferRmiAddress =
                Helper.getProcessRmiAddressPreffixFor(talkingToProcess);
        remoteReceiveBufferRmiAddress += "receiveBuffer";
        try
        {
            Logger.getLogger(ReceiveThread.class.getName()).log(
                Level.INFO, "connecting to: " + remoteReceiveBufferRmiAddress);
            iReceiveThread = (IReceiveThread) Naming.lookup(
                    remoteReceiveBufferRmiAddress);
            while(running)
            {
                emit();
            }
        } catch (NotBoundException ex)
        {
            Logger.getLogger(EmittingThread.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MalformedURLException ex)
        {
            Logger.getLogger(EmittingThread.class.getName()).log(Level.SEVERE, null, ex);
        } catch (RemoteException ex)
        {
            Logger.getLogger(EmittingThread.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
